package cn.edu.sjtu.se.dslab.component.design.generate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import cn.edu.sjtu.se.dslab.component.dp.parse.DesignPatternInstanceBean;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.util.FileUtil;



/**
 * 
 * @author longshaohang
 *
 */
public abstract class DesignPatternCodeGenerator {
	private static final Logger logger = Logger.getLogger(DesignPatternCodeGenerator.class);
	//public static final String GENERATE_PATH = "/generate";
	//public static final String CODE_PATH = "/generate/code";
	public static final String TEMPLATE_PATH = "systemdata/velocity_template/bus";
	//public static final String XML_PATH = "/generate/xml";
	
	private VelocityEngine ve = null;
	private VelocityContext vc = null;
	private Template template = null;
	
	public abstract void generateCode(String targetDirPath, Project project, DesignPatternInstanceBean ...dpiBean) throws IOException;
	//public abstract void packCodeIntoZip(String sourceDirPath,String zipFilePath, String zipFileName) throws IOException;

	public DesignPatternCodeGenerator(){
		//System.out.println(VelocityEngine.FILE_RESOURCE_LOADER_PATH);
		ve = new VelocityEngine();
		String templateDir = FileUtil.getRootPath() + TEMPLATE_PATH;
		//System.out.println(templateDir);
		Properties properties = new Properties();
		properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, templateDir);
		ve.init(properties);
		vc = new VelocityContext();
	}
	
	protected void inputData(String key, Object value){
		vc.put(key, value);
	}

//	private File getFile(String targetFileName) throws IOException{
//		System.out.println(targetFileName);
//		File targetFile = new File(targetFileName);
//		if (!targetFile.exists()){
//			targetFile.createNewFile();
//			System.out.println("create file:" + targetFile.getName());
//		}
//		return targetFile;
//	}

	protected void generateCodeFile(String targetFilePath, String templateFile){
		logger.info("DesignPatternCodeGenerator :generate code file: "+ targetFilePath);
		//BufferedWriter writer = null;
		FileWriter fWriter = null;
		OutputStreamWriter outWriter = null;
		try{
			//writer = new BufferedWriter(new OutputStreamWriter(System.out));
			//File writeFile = getFile(targetFileName);
			File writeFile = FileUtil.getOldOrCreateFile(targetFilePath);
			fWriter = new FileWriter(writeFile);
			//transfer to GBK encoding
			outWriter = new OutputStreamWriter(new FileOutputStream(writeFile), "GBK");  
			
			if (loadTemplage(templateFile)){
				//template.merge(vc, fWriter);
				template.merge(vc, outWriter);
			}
			
		}catch(IOException ex){
			ex.printStackTrace();
		}finally{
			try{
				//writer.flush();
				//writer.close();
				//fWriter.flush();
				//fWriter.close();
				outWriter.flush();
				outWriter.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}

	private boolean loadTemplage(String templateFile) {
		template = ve.getTemplate(templateFile);
		if (template != null) return true;
		return false;
	}
}
